package com.centling.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.centling.business.OrderVO;
import com.centling.core.BaseDAO;
import com.centling.dao.IOrderDAO;
import com.centling.util.LocalUtils;

@Repository
@Transactional
public class OrderDAO extends BaseDAO implements IOrderDAO{
	
	private static Logger log = Logger.getLogger(OrderDAO.class);
	
	/**
	 * 
	 * @param vo			订单信息
	 * @param customerid	客户ID
	 * @param orderid		订单ID
	 * @param FCurrencyID	币别
	 */
	@Transactional
	public void save(OrderVO vo, Integer customerid, Integer orderid, Integer fCurrencyID, 
			String fExchangeRate, Integer fEmployee, Integer fDepartment) {
		log.info("save order invoke...");
		Session session = this.sessionFactory.getCurrentSession();
		StringBuffer hql = new StringBuffer();
		hql.append("insert into t_RPContract(");
		
		hql.append("FContractID,FCustomer");
		if(LocalUtils.isNotBlank(vo.getFtotalamountfor()))			hql.append(",Ftotalamountfor");
		if(LocalUtils.isNotBlank(vo.getF_customer_source()))		hql.append(",F_customer_source");
		if(LocalUtils.isNotBlank(vo.getF_order_number()))			hql.append(",F_order_number");
		if(LocalUtils.isNotBlank(vo.getF_system_id()))				hql.append(",F_system_id");
		if(LocalUtils.isNotBlank(vo.getF_user_name()))				hql.append(",F_user_name");
		if(LocalUtils.isNotBlank(vo.getF_customer_number()))		hql.append(",F_customer_number");
		if(LocalUtils.isNotBlank(vo.getF_product()))				hql.append(",F_product");
		if(LocalUtils.isNotBlank(vo.getF_fabric()))					hql.append(",F_fabric");
		if(LocalUtils.isNotBlank(vo.getF_date()))					hql.append(",F_date");
		if(LocalUtils.isNotBlank(vo.getF_deliver_date()))			hql.append(",F_deliver_date");
		if(LocalUtils.isNotBlank(vo.getF_delivery_date()))			hql.append(",F_delivery_date");
		if(LocalUtils.isNotBlank(vo.getF_order_status()))			hql.append(",F_order_status");
		if(LocalUtils.isNotBlank(vo.getF_pay_status()))				hql.append(",F_pay_status");
		if(LocalUtils.isNotBlank(vo.getfName()))					hql.append(",FName");
		if(LocalUtils.isNotBlank(vo.getFsex()))						hql.append(",FSex");
		if(LocalUtils.isNotBlank(vo.getFphone()))					hql.append(",FPone");
		if(LocalUtils.isNotBlank(vo.getFnote()))					hql.append(",FNOTE");
		if(LocalUtils.isNotNull(fEmployee))							hql.append(",FEmployee");
		if(LocalUtils.isNotNull(fDepartment))						hql.append(",FDepartment");
		

		hql.append(",FCurrencyID,FExchangeRate,FContractTypeID,FTotalAmount,FRP) values(:FContractID,:FCustomer");
		if(LocalUtils.isNotBlank(vo.getFtotalamountfor()))			hql.append(",:Ftotalamountfor");
		if(LocalUtils.isNotBlank(vo.getF_customer_source()))		hql.append(",:F_customer_source");
		if(LocalUtils.isNotBlank(vo.getF_order_number()))			hql.append(",:F_order_number");
		if(LocalUtils.isNotBlank(vo.getF_system_id()))				hql.append(",:F_system_id");
		if(LocalUtils.isNotBlank(vo.getF_user_name()))				hql.append(",:F_user_name");
		if(LocalUtils.isNotBlank(vo.getF_customer_number()))		hql.append(",:F_customer_number");
		if(LocalUtils.isNotBlank(vo.getF_product()))				hql.append(",:F_product");
		if(LocalUtils.isNotBlank(vo.getF_fabric()))					hql.append(",:F_fabric");
		if(LocalUtils.isNotBlank(vo.getF_date()))					hql.append(",:F_date");
		if(LocalUtils.isNotBlank(vo.getF_deliver_date()))			hql.append(",:F_deliver_date");
		if(LocalUtils.isNotBlank(vo.getF_delivery_date()))			hql.append(",:F_delivery_date");
		if(LocalUtils.isNotBlank(vo.getF_order_status()))			hql.append(",:F_order_status");
		if(LocalUtils.isNotBlank(vo.getF_pay_status()))				hql.append(",:F_pay_status");
		if(LocalUtils.isNotBlank(vo.getfName()))					hql.append(",:FName");
		if(LocalUtils.isNotBlank(vo.getFsex()))						hql.append(",:FSex");
		if(LocalUtils.isNotBlank(vo.getFphone()))					hql.append(",:FPhone");
		if(LocalUtils.isNotBlank(vo.getFnote()))					hql.append(",:FNOTE");
		if(LocalUtils.isNotNull(fEmployee))							hql.append(",:FEmployee");
		if(LocalUtils.isNotNull(fDepartment))						hql.append(",:FDepartment");
		hql.append(",:FCurrencyID,:FExchangeRate,-1,0,0)");
		
		log.info("hql: "+hql.toString());
		Query query = session.createSQLQuery(hql.toString());
		
		query.setParameter("FContractID", orderid);
		query.setParameter("FCustomer", customerid);		
		if(LocalUtils.isNotBlank(vo.getFtotalamountfor()))			query.setParameter("Ftotalamountfor", vo.getFtotalamountfor());
		if(LocalUtils.isNotBlank(vo.getF_customer_source()))		query.setParameter("F_customer_source", vo.getF_customer_source());
		if(LocalUtils.isNotBlank(vo.getF_order_number()))			query.setParameter("F_order_number", vo.getF_order_number());
		if(LocalUtils.isNotBlank(vo.getF_system_id()))				query.setParameter("F_system_id", vo.getF_system_id());
		if(LocalUtils.isNotBlank(vo.getF_user_name()))				query.setParameter("F_user_name", vo.getF_user_name());
		if(LocalUtils.isNotBlank(vo.getF_customer_number()))		query.setParameter("F_customer_number", vo.getF_customer_number());
		if(LocalUtils.isNotBlank(vo.getF_product()))				query.setParameter("F_product", vo.getF_product());
		if(LocalUtils.isNotBlank(vo.getF_fabric()))					query.setParameter("F_fabric", vo.getF_fabric());
		if(LocalUtils.isNotBlank(vo.getF_date()))					query.setParameter("F_date", vo.getF_date());
		if(LocalUtils.isNotBlank(vo.getF_deliver_date()))			query.setParameter("F_deliver_date", vo.getF_deliver_date());
		if(LocalUtils.isNotBlank(vo.getF_delivery_date()))			query.setParameter("F_delivery_date", vo.getF_delivery_date());
		if(LocalUtils.isNotBlank(vo.getF_order_status()))			query.setParameter("F_order_status", vo.getF_order_status());
		if(LocalUtils.isNotBlank(vo.getF_pay_status()))				query.setParameter("F_pay_status", vo.getF_pay_status());
		if(LocalUtils.isNotBlank(vo.getfName()))					query.setParameter("FName", vo.getfName());
		if(LocalUtils.isNotBlank(vo.getFsex()))						query.setParameter("FSex", vo.getFsex());
		if(LocalUtils.isNotBlank(vo.getFphone()))					query.setParameter("FPhone", vo.getFphone());
		if(LocalUtils.isNotBlank(vo.getFnote()))					query.setParameter("FNOTE", vo.getFnote());	
		if(LocalUtils.isNotNull(fEmployee))							query.setParameter("FEmployee", vo.getFnote());
		if(LocalUtils.isNotNull(fDepartment))						query.setParameter("FDepartment", vo.getFnote());
		query.setParameter("FCurrencyID", fCurrencyID);	
		query.setParameter("FExchangeRate", fExchangeRate);
		
		query.executeUpdate();
		
		log.info("save order end.");
	}

	/**
	 * 查询出	订单状态	不为已发货的订单ID
	 */
	public List<String> getNoDeliveryOrderIDs() {
		Session session = this.sessionFactory.getCurrentSession();
		StringBuffer hql = new StringBuffer();
		hql.append("SELECT F_system_id FROM t_RPContract where F_order_status <> '已发货'");
		Query query = session.createSQLQuery(hql.toString());
		List<String> list = new ArrayList<String>();
		for (Object o : query.list()){
			if (o.toString().trim().length()!=0){
				list.add(o.toString());
			}
		}
		return list;
	}

	public void updateOrder(String systemid, String status) {
		log.info("updateOrder invoke...");
		Session session = this.sessionFactory.getCurrentSession();
		StringBuffer hql = new StringBuffer();
		hql.append("update t_RPContact ");
		hql.append("set F_order_status=:F_order_status ");
		hql.append("where F_system_id=:F_system_id");
		session.createSQLQuery(hql.toString())
			.setParameter("F_order_status", status)
			.setParameter("F_system_id", systemid)
			.executeUpdate();
		log.info("updateOrder end.");
	}
}
